<?php


namespace app\api\server\user;


use app\api\model\order\OrderPayModel;
use app\api\model\view\OrderOpus_Opus_OpusNumber_buyer_writerView;
use app\api\ServerBase;

class OrderServer extends ServerBase
{
    protected $model;
    function __construct($request)
    {
        $this->request=$request;
        $this->model=new OrderPayModel();
    }
    function buyOrder(){
        $this->request['type']=1;
        $res=$this->model->getList($this->request,'*',$this->getLimit());
        if(is_array($res)){
            $view=new OrderOpus_Opus_OpusNumber_buyer_writerView();
            $field='opus_id,opus_title,opus_img,opus_number,seller_nickname,seller_true_name';
            foreach ($res['data'] as $key=>&$val){
                $val['opus']=$view->getList(['order_pay_id'=>$val['id']],$field,99999,'id asc',false);

            }
            unset($val);
        }
        show_json('code_common.success',$res);
    }
    
    function buyOrderDetail(){
        global $user;
        if(!check_request($this->request,'id')){
            show_json('code_common.illegal');
        }
        $order=(new OrderPayModel())->getFindById($this->request['id']);
        
        if(empty($order)){
            show_json('code_common.not_find');
        }
        if($order['buyer_id']!=$user['id']){
            show_json('wxapp.OrderDetail.noBuyer');
        }
        $view=new OrderOpus_Opus_OpusNumber_buyer_writerView();
        $opus=$view->getList(['order_pay_id'=>$order['id']],'opus_id,opus_title,opus_img,opus_number,order_price,seller_nickname,seller_true_name',99999,'id asc',false);
        $order['opus']=$opus;
        if(!empty($order['create_time']) && (strtotime($order['create_time'])+60*10)>time()){
			$order['time']=(strtotime($order['create_time'])+60*10)-time();
		}else{
			$order['time']=-1;
		}
        if(!empty($order['pay_time'])){
            $order['pay_time']=date('Y-m-d H:i:s',$order['pay_time']);
        }
        if(!empty($order['cancel_time'])){
            $order['cancel_time']=date('Y-m-d H:i:s',$order['cancel_time']);
        }
        show_json('code_common.success',$order);
    }
    
    function sellOrder(){
        $this->request['type']=2;
        $this->request['status']=1;
        $res=$this->model->getList($this->request,'*',$this->getLimit());
        if(is_array($res)){
            $view=new OrderOpus_Opus_OpusNumber_buyer_writerView();
            $field='opus_id,opus_title,opus_img,opus_number,seller_nickname,seller_true_name';
            foreach ($res['data'] as $key=>&$val){
                $val['opus']=$view->getList(['order_pay_id'=>$val['id'],'status'=>1],$field,99999,'id asc',false);
            }
            unset($val);
        }
        show_json('code_common.success',$res);
    }
    //卖出订单，只显示已支付的
    function sellOrderDetail(){
        global $user;
        if(!check_request($this->request,'id')){
            show_json('code_common.illegal');
        }
        $order=(new OrderPayModel())->getFindById($this->request['id']);
        if(empty($order)||$order['status']!=1){
            show_json('code_common.not_find');
        }
        if($order['seller_id']!=$user['id']){
            show_json('wxapp.OrderDetail.noSeller');
        }
        if(!empty($order['pay_time'])){
            $order['pay_time']=date('Y-m-d H:i:s',$order['pay_time']);
        }
        if(!empty($order['cancel_time'])){
            $order['cancel_time']=date('Y-m-d H:i:s',$order['cancel_time']);
        }
        $view=new OrderOpus_Opus_OpusNumber_buyer_writerView();
        $opus=$view->getList(['order_pay_id'=>$order['id']],'opus_id,opus_title,opus_img,opus_number,order_price,seller_nickname,seller_true_name',99999,'id asc',false);
        $order['opus']=$opus;
        show_json('code_common.success',$order);
    }
}